import 'package:cashbook/model/user_vo.dart';
import 'package:cashbook/utils/manager.dart';
import 'package:cashbook/utils/toast.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return LoginState();
  }
}

class LoginState extends State<LoginPage> {
  var name = TextEditingController();
  var pasd = TextEditingController();

  var nameFocusNode = FocusNode();
  var pasdFocusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    var textEditOutlineInputBorder = OutlineInputBorder(
      borderRadius: BorderRadius.circular(5),
      borderSide: BorderSide(color: Theme.of(context).primaryColor),
    );
    var testStyle =
        TextStyle(color: Colors.blue, decoration: TextDecoration.underline);
    return Scaffold(
      body: Center(
        child: Stack(
          children: <Widget>[
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  child: Text(
                    '登录',
                    style: TextStyle(
                        fontWeight: FontWeight.w900,
                        fontSize: 30,
                        fontFamily: 'SIMYOU'),
                  ),
                  margin: const EdgeInsets.only(bottom: 20),
                ),
                Container(
                  margin: const EdgeInsets.only(left: 20, right: 20, top: 15),
                  child: TextField(
                    controller: name,
                    focusNode: nameFocusNode,
                    decoration: InputDecoration(
                        hintText: '请输入用户名',
                        //// 输入框文本周边的距离
                        contentPadding: const EdgeInsets.all(12),
                        labelText: '用户名',
                        border: textEditOutlineInputBorder),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(
                      left: 20, right: 20, top: 15, bottom: 15),
                  child: TextField(
                    controller: pasd,
                    focusNode: pasdFocusNode,
                    decoration: InputDecoration(
                        hintText: '请输入密码',
                        //// 输入框文本周边的距离
                        contentPadding: const EdgeInsets.all(12),
                        labelText: '密码',
                        border: textEditOutlineInputBorder),
                  ),
                ),
                Container(
                  width: 200,
                  child: RaisedButton(
                    onPressed: () {
                      loginAction();
                    },
                    color: Colors.blue,
                    child: Text('登录'),
                  ),
                )
              ],
            ),
            Container(
              alignment: Alignment.bottomLeft,
              margin: EdgeInsets.only(left: 20, bottom: 20),
              child: GestureDetector(
                onTap: () {
                  clearFours();
                  //Toast.toast(context, '注册');
                  Navigator.of(context).pushNamed('/register');
                },
                child: Text(
                  '注册',
                  style: testStyle,
                ),
              ),
            ),
            Container(
              alignment: Alignment.bottomLeft,
              margin: EdgeInsets.only(left: 55, bottom: 20),
              child: GestureDetector(
                onTap: () {
                  clearFours();
                  Navigator.of(context).pushNamed('/forgot');
                },
                child: Text(
                  '找回密码',
                  style: testStyle,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void loginAction() async {
    if (name.text.length == 0) {
      Toast.toast(context, '请填写一个用户名');
      return;
    }
    if (pasd.text.length == 0) {
      Toast.toast(context, '请填写密码');
      return;
    }
    User loginUser = User();
    loginUser.name = this.name.text.toString();
    loginUser.psd = this.pasd.text.toString();

    clearFours();

    User resUser = await UserProvider().findByName(loginUser);
    if (null == resUser) {
      Toast.toast(context, '没有找到${loginUser.name}这个用户');
      return;
    }
    if (loginUser.psd != resUser.psd) {
      pasd.text = '';
      Toast.toast(context, '密码错误！');
      return;
    } else {
      //设置全局标识
      Manager.instance.isLogin = true;
      //保存用户数据
      Manager.instance.loginUser = resUser;
      //跳转到主界面
      Navigator.popAndPushNamed(context, '/home');
    }
  }

  //清楚焦点  关闭键盘
  void clearFours() {
    pasdFocusNode.unfocus();
    nameFocusNode.unfocus();
  }
}
